package com.example.ibsp_demo.controller.create;

import com.example.ibsp_demo.mapper.content.ContentMapper;
import com.example.ibsp_demo.mapper.content.FavoriteMapper;
import com.example.ibsp_demo.pojo.entity.content.Favorite;
import com.example.ibsp_demo.pojo.response.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/favorite")
@CrossOrigin(origins = "http://localhost:8080")
public class FavoriteController {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private ContentMapper contentMapper;

    // 收藏内容
    @PostMapping("/add")
    public ResponseMessage<String> addFavorite(@RequestParam Integer uid, @RequestParam Integer ctid) {
        try {
            Favorite existing = favoriteMapper.selectByUidAndCtid(uid, ctid);
            if (existing != null) {
                return ResponseMessage.error(500, "您已收藏过该内容");
            }

            Favorite favorite = new Favorite();
            favorite.setUid(uid);
            favorite.setCtid(ctid);
            favorite.setFavoriteTime(new Date());

            favoriteMapper.insert(favorite);
            contentMapper.updateFavoriteCount(ctid, 1);

            return ResponseMessage.success("收藏成功");

        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈
            return ResponseMessage.error(500, "服务器内部错误：" + e.getMessage());
        }
    }

    // 取消收藏
    @PostMapping("/remove")
    public ResponseMessage<String> removeFavorite(@RequestParam Integer uid, @RequestParam Integer ctid) {
        try {
            if (uid == null || ctid == null) {
                return ResponseMessage.error(500, "参数不能为空");
            }
            favoriteMapper.deleteByUidAndCtid(uid, ctid);
            contentMapper.updateFavoriteCount(ctid, -1);
            return ResponseMessage.success("取消收藏成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseMessage.error(500, "取消收藏失败: " + e.getMessage());
        }
    }

    // 查询用户收藏
    @GetMapping("/user")
    public ResponseMessage<List<Favorite>> getFavoritesByUser(@RequestParam Integer uid) {
        List<Favorite> favorites = favoriteMapper.selectByUid(uid);
        return ResponseMessage.success(favorites);
    }

    // 查询内容收藏者
    @GetMapping("/content")
    public ResponseMessage<List<Favorite>> getFavoritesByContent(@RequestParam Integer ctid) {
        List<Favorite> favorites = favoriteMapper.selectByCtid(ctid);
        return ResponseMessage.success(favorites);
    }

    @GetMapping("/check")
    public ResponseMessage<Boolean> checkFavoriteStatus(
            @RequestParam Integer uid,
            @RequestParam Integer ctid) {
        try {
            Favorite favorite = favoriteMapper.selectByUidAndCtid(uid, ctid);
            return ResponseMessage.success(favorite != null);
        } catch (Exception e) {
            return ResponseMessage.error(500, "检查收藏状态失败: " + e.getMessage());
        }
    }
}